//
//  PopoverContent.swift
//  ZJTools
//
//  Created by 万孟超 on 2023/10/23.
//

import Cocoa
import SnapKit

class PopoverContent: NSView {

    override func draw(_ dirtyRect: NSRect) {
        super.draw(dirtyRect)

        // Drawing code here.
    }
    
    var dataArray: [String] = []
    
    init(dataArray: [String]) {
        self.dataArray = dataArray
        super.init(frame: .zero)
        installSubview()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func installSubview() {
        var lastLabel: NSTextField?
        for (idx, data) in dataArray.enumerated() {
            let label = createLabel(data)
            addSubview(label)
            label.snp.makeConstraints { make in
                if lastLabel != nil {
                    make.top.equalTo(lastLabel!.snp.bottom).offset(15)
                }else{
                    make.top.equalTo(15)
                }
                make.width.lessThanOrEqualTo(510)
                make.left.equalTo(15)
                make.right.equalTo(-15)
                if idx == dataArray.count - 1 {
                    make.bottom.equalTo(-15)
                }
            }
            lastLabel = label
        }
    }
    
    func createLabel(_ title: String) -> NSTextField {
        let titleLabel: NSTextField = {
            let tf = NSTextField()
            tf.isVerticalCenter = true
            tf.font = .systemFont(ofSize: 14)
            tf.isBordered = false
            tf.isEditable = false
            tf.isSelectable = true
            tf.textColor = .black
            tf.bezelStyle = .roundedBezel
            tf.stringValue = title
            tf.backgroundColor = .clear
            return tf
        }()
        return titleLabel
    }
}
